package com.scau402.system.service.impl;

import java.util.List;

import com.scau402.common.annotation.DataScope;
import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.common.utils.DateUtils;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.common.utils.StringUtils;
import com.scau402.common.utils.code.ClientCodeUtils;
import com.scau402.common.utils.code.SupplierCodeUtils;
import com.scau402.system.domain.SysTenant;
import com.scau402.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scau402.system.mapper.WmsBasicSupplierMapper;
import com.scau402.system.domain.WmsBasicSupplier;
import com.scau402.system.service.IWmsBasicSupplierService;

/**
 * 供应商管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-26
 */
@Service
public class WmsBasicSupplierServiceImpl implements IWmsBasicSupplierService 
{
    @Autowired
    private WmsBasicSupplierMapper wmsBasicSupplierMapper;

    @Autowired
    private ISysTenantService tenantService;

    /**
     * 查询供应商管理
     * 
     * @param id 供应商管理主键
     * @return 供应商管理
     */
    @Override
    @DataScope(tenantAlias = "t", userAlias = "u")
    public WmsBasicSupplier selectWmsBasicSupplierById(Long id)
    {
        return wmsBasicSupplierMapper.selectWmsBasicSupplierById(id);
    }

    /**
     * 查询供应商管理列表
     * 
     * @param wmsBasicSupplier 供应商管理
     * @return 供应商管理
     */
    @Override
    @DataScope(tenantAlias = "t", userAlias = "u")
    public List<WmsBasicSupplier> selectWmsBasicSupplierList(WmsBasicSupplier wmsBasicSupplier)
    {
        wmsBasicSupplier.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        return wmsBasicSupplierMapper.selectWmsBasicSupplierList(wmsBasicSupplier);
    }

    /**
     * 新增供应商管理
     * 
     * @param wmsBasicSupplier 供应商管理
     * @return 结果
     */
    @Override
    public int insertWmsBasicSupplier(WmsBasicSupplier wmsBasicSupplier)
    {
        wmsBasicSupplier.setCreateTime(DateUtils.getNowDate());
        wmsBasicSupplier.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        return wmsBasicSupplierMapper.insertWmsBasicSupplier(wmsBasicSupplier);
    }

    /**
     * 修改供应商管理
     * 
     * @param wmsBasicSupplier 供应商管理
     * @return 结果
     */
    @Override
    public int updateWmsBasicSupplier(WmsBasicSupplier wmsBasicSupplier)
    {
        wmsBasicSupplier.setUpdateTime(DateUtils.getNowDate());
        return wmsBasicSupplierMapper.updateWmsBasicSupplier(wmsBasicSupplier);
    }

    /**
     * 批量删除供应商管理
     * 
     * @param ids 需要删除的供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsBasicSupplierByIds(Long[] ids)
    {
        return wmsBasicSupplierMapper.deleteWmsBasicSupplierByIds(ids);
    }

    /**
     * 删除供应商管理信息
     * 
     * @param id 供应商管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsBasicSupplierById(Long id)
    {
        return wmsBasicSupplierMapper.deleteWmsBasicSupplierById(id);
    }

    @Override
    public String getMaxCode(SysUser user) {
        Long tenantId = user.getTenantId();
        return wmsBasicSupplierMapper.getMaxCode(tenantId);
    }

    @Override
    public String getSupplierCode(String loginUserTenantCode) throws InterruptedException {
        SupplierCodeUtils supplierCodeUtils = new SupplierCodeUtils();
        return supplierCodeUtils.getNewCode(loginUserTenantCode);
    }

    @Override
    public String getNewCode(SysUser user) throws InterruptedException {
        // 获取数据库中最大的编码
        String maxCode = getMaxCode(user);

        // 返回maxCode为NULL，及当前数据库没有数据，从0开始
        if(StringUtils.isNull(maxCode)) {
            maxCode = "0";
        }

        // 获取当前用户所属租户编码
        Long loginUserTenantId = SecurityUtils.getLoginUser().getTenantId();
        String loginUserTenantCode = tenantService.selectSysTenantByTenantId(loginUserTenantId).getTenantCode();

        // 设置redis的当前编码
        SupplierCodeUtils supplierCodeUtils = new SupplierCodeUtils();
        supplierCodeUtils.setCurrentCodeToMax(maxCode, loginUserTenantCode);
        return supplierCodeUtils.getNewCode(loginUserTenantCode);
    }

    @Override
    public boolean checkNameUnique(WmsBasicSupplier wmsBasicSupplier) {
        if(wmsBasicSupplierMapper.checkNameUnique(wmsBasicSupplier) > 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean checkCodeUnique(WmsBasicSupplier wmsBasicSupplier) {
        if(wmsBasicSupplierMapper.checkCodeUnique(wmsBasicSupplier) > 0) {
            return false;
        }
        return true;
    }

    @Override
    public int startOrStop(List<WmsBasicSupplier> wmsBasicSupplierList) {
        return wmsBasicSupplierMapper.startOrStopByIds(wmsBasicSupplierList);
    }
}
